package com.ytjj.qmyx.mall.service.h5.impl;

import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationResponse;
import com.ytjj.qmyx.mall.mapper.UsersRealNameMapper;
import com.ytjj.qmyx.mall.model.UsersRealName;
import com.ytjj.qmyx.mall.model.UsersRealNameExample;
import com.ytjj.qmyx.mall.model.request.UsersRealNameRequest;
import com.ytjj.qmyx.mall.service.h5.H5RealNameAuthService;
import com.ytjj.qmyx.mall.utils.IdCardVerificationUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class H5RealNameAuthServiceImpl implements H5RealNameAuthService {

    @Autowired
    private UsersRealNameMapper usersRealNameMapper;


    @Override
    public int save(UsersRealNameRequest realName) {
        UsersRealName usersRealName = new UsersRealName();
        BeanUtils.copyProperties(realName,usersRealName);
        //最多只能添加15个人
        UsersRealNameExample realNameExample = new UsersRealNameExample();
        realNameExample.createCriteria().andUserIdEqualTo(realName.getUserId());
        long totaUser = usersRealNameMapper.countByExample(realNameExample);
        Assert.isTrue(totaUser<15,"一个账号最多只能添加15个人");

        //不允许重复添加
        int countUser = usersRealNameMapper.countUser(usersRealName);
        Assert.isTrue(countUser==0,"已经添加过此订购人,请勿重复添加");

        IdCardVerificationResponse response = IdCardVerificationUtil.checkIdCard(realName.getCard(),realName.getName());
        Assert.notNull(response,"调用第三方身份识别接口出错");
        Assert.isTrue("0".equals(response.getResult()),response.getDescription());

        UsersRealNameExample example = new UsersRealNameExample();
        example.createCriteria().andUserIdEqualTo(realName.getUserId()).andDefaultSelectEqualTo(1);
        long total = usersRealNameMapper.countByExample(example);
        if (total == 0){
            usersRealName.setDefaultSelect(1);
        }
        int count = usersRealNameMapper.insertSelective(usersRealName);
        Assert.isTrue(count>0,"保存失败");
        return usersRealName.getId();
    }


    @Override
    public List<UsersRealName> findList(Integer userId) {
        return usersRealNameMapper.findList(userId);
    }

    @Transactional
    @Override
    public void setDefaultSelect(UsersRealName realName) {
        realName.setDefaultSelect(1);
        usersRealNameMapper.updateByPrimaryKeySelective(realName);
        //其他的设置为非默认
        UsersRealNameExample example = new UsersRealNameExample();
        example.createCriteria().andUserIdEqualTo(realName.getUserId()).andIdNotEqualTo(realName.getId());

        UsersRealName usersRealName = new UsersRealName();
        usersRealName.setDefaultSelect(0);
        usersRealNameMapper.updateByExampleSelective(usersRealName,example);
    }

    @Override
    public UsersRealName getById(Integer id) {
        UsersRealName realName = usersRealNameMapper.selectByPrimaryKey(id);
        if (realName!=null && realName.getCard()!=null){
            String card = realName.getCard().replace(realName.getCard().substring(6,14),"*******");
            realName.setCard(card);
        }
        return realName;
    }

    @Override
    public void update(UsersRealNameRequest realName) {
        IdCardVerificationResponse response = IdCardVerificationUtil.checkIdCard(realName.getCard(),realName.getName());
        Assert.notNull(response,"调用第三方身份识别接口出错");
        Assert.isTrue("0".equals(response.getResult()),response.getDescription());

        UsersRealName usersRealName = new UsersRealName();
        BeanUtils.copyProperties(realName,usersRealName);
        usersRealNameMapper.updateByPrimaryKeySelective(usersRealName);
    }

    @Override
    public void delete(Integer id) {
        usersRealNameMapper.deleteByPrimaryKey(id);
    }
}
